package island;

public class Solution {
    public int numIslands(char[][] grid) {
        //
        if (grid ==null || grid.length<=0 || grid[0].length<=0)
            return 0;
        int row = grid.length;
        int column =grid[0].length;
        int numOfIsland =0;
        //
        for (int i = 0; i < row; i++) {
            for (int j = 0; j < column; j++) {
                if (grid[i][j] == 1){
                    dfs(grid,i,j);
                    numOfIsland++;
                }
            }
        }

        return numOfIsland;
    }

    private void dfs(char[][] grid, int i, int j) {
        //判断在地图
        if (i<0 || j<0 || i>= grid.length || j >= grid[0].length || grid[i][j] !=1 )
            return;

        grid[i][j] = '2';

        dfs(grid,i+1,j);
        dfs(grid,i-1,j);
        dfs(grid,i,j+1);
        dfs(grid,i,j-1);
    }
}
